package com.hsd.controller;

import com.hsd.pojo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @BelongsProject: hsd_1_calculator
 * @BelongsPackage: com.hsd.controller
 * @Author: Heng_Xin
 * @CreateTime: 2025-04-17  19:55
 * @Description: 三角函数控制器
 * @Version: 1.0
 */

@RestController
@RequestMapping("/calculate/tf")
public class TrigonometryController {

    /**
     * @description: 计算 sin(num), num 是角度
     * @author: Heng_Xin
     * @date: 2025/4/17 20:10
     * @param: request
     * @return: Result
     **/
    @PostMapping("/sin")
    public Result sin(@RequestBody Map<String, Double> request) {
        Double num = request.get("num");
        if (num == null) {
            return Result.error("输入不能为空");
        }
        // 需要转换为弧度
        Double res = Math.sin(Math.toRadians(num));
        // @todo Heng_Xin 可能要考虑四舍五入
        return Result.success(res);
    }

    /**
     * @description: 计算 cos(num), num 是角度
     * @author: Heng_Xin
     * @date: 2025/4/17 20:11
     * @param: request
     * @return: Result
     **/
    @PostMapping("/cos")
    public Result cos(@RequestBody Map<String, Double> request) {
        Double num = request.get("num");
        if (num == null) {
            return Result.error("输入不能为空");
        }
        Double res = Math.cos(Math.toRadians(num));
        return Result.success(res);
    }

    /**
     * @description: 计算 tan(num), num 是角度
     * @author: Heng_Xin
     * @date: 2025/4/17 20:11
     * @param: request
     * @return: Result
     **/
    @PostMapping("/tan")
    public Result tan(@RequestBody Map<String, Double> request) {
        Double num = request.get("num");
        if (num == null) {
            return Result.error("输入不能为空");
        }
        // 特别的, tan(k * PI / 2) -> +/-无穷的
        // 不过它没有抛出异常, 而是返回 1.633123935319537E16
        // 那应该问题不大
        Double res = Math.tan(Math.toRadians(num));
        return Result.success(res);
    }
}
